﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

//5.让编译器看到PersonFriend类声明；
template<class T1, class T2>class PersonFriend;
//4.让编译器提前看到printPerson声明；
template<class T1, class T2>void printPerson(PersonFriend<T1, T2>& p);

template<class T1, class T2>
class PersonFriend
{
	//1.友元函数类内实现；普通函数的声明；
	//friend void printPerson(PersonFriend<T1, T2>& p);

	//3.利用空参数列表，告诉编译器，这是模板函数的声明；
	friend void printPerson<>(PersonFriend<T1, T2>& p);
	/*{
		cout << "姓名：" << p.m_Name << " ，年龄：" << p.m_Age << endl;
	}*/
public:
	PersonFriend(T1 name, T2 age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
private:
	T1 m_Name;
	T2 m_Age;
};
//2.友元函数类外实现；
template<class T1, class T2>
void printPerson(PersonFriend<T1, T2>& p)
{
	cout << "姓名：" << p.m_Name << " ，年龄：" << p.m_Age << endl;
}
void testTemplateFriendOut()
{
	PersonFriend<string, int>p("Tom", 10);
	printPerson(p);//姓名：Tom ，年龄：10
}

//main307：友元碰到类模板友元函数类外实现
int main307()
{
	testTemplateFriendOut();

	system("pause");
	return EXIT_SUCCESS;
}